<?php
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Model\Backstage\User;
use App\Tool\Error;
use Illuminate\Http\Request;
use App\Model\Backstage\Achievement;
use App\Model\Backstage\RoomInfo;
use App\Model\Backstage\Customers;
use App\Model\Backstage\RoomFollow;
use App\Model\Backstage\CustomersFollow;
/**
 * Class AchievementHouseController
 * @package App\Http\Controllers\Backstage
 * 业绩排行
 * date:2018-6-09
 * author:lxy
 */
class AchievementController extends Controller
{

    protected $achievement_model;  //业绩model
    protected $roomInfo_model;    //房源model
    protected $customers_model;  //客源model
    protected $roomFollow_model;  //房源跟进model
    protected $customersFollow_model; //客源跟进model
//    protected $begin_month_time;  //当前月份开始时间
    public function __construct()
    {
//        $this->begin_month_time = mktime ( 0, 0, 0, date ( "m" ), 1, date ( "Y" ) ) ;  //当月开始时间戳
        $this->achievement_model     = new Achievement();
        $this->roomInfo_model        = new RoomInfo();
        $this->customers_model       = new Customers();
        $this->roomFollow_model      = new RoomFollow();
        $this->customersFollow_model = new CustomersFollow();
    }

    /**
     * 获取业绩排行统计
     */
    public function getAchievementList(Request $request)
    {
        $param = $request->all();
        $map = [];
        $where = [];
        //开始时间
        if ($request->input('start_time')) {
            $map[] = ['a_created_at', '>=', strtotime($param['start_time'].' 00:00:00')];
        } else {
            $map[] = ['a_created_at', '>=', strtotime(date('Y-m-d',strtotime('-1 month')).' 00:00:00')];
        }
        //结束时间
        if ($request->input('end_time')) {
            $map[] = ['a_created_at', '<=', strtotime($param['end_time'].' 23:59:59')];
        } else {
            $map[] = ['a_created_at', '<=', time()];
        }
        //部门id
        if ($request->input('u_d_id')) {
            $where[] = ['u_d_id', $request->input('u_d_id')];
        }
        //员工ID
        if ($request->input('u_id')) {
            $where[] = ['u_id', $request->input('u_id')];
        }
        $res = $this->achievement_model->getAchievementList($map, $where);
        $start = date('Y-m-d',strtotime('-1 month'));
        $end = date('Y-m-d',time());
        $data = [];
        foreach ($res as $k=>$v) {
            $num = $k+1;
            $v['ranking'] = '第'.$num.'名';
           array_push($data, $v);
        }

        return $this->success('ok', $data, $start, $end);
    }

    /**
     * 获取员工录入房源排行
     */
    public function getRoomInfoAchievementList(Request $request){
        $param = $request->all();
        $map = [];
        $where = [];
        //开始时间
        if ($request->input('start_time')) {
            $map[] = ['r_created_at', '>=', strtotime($param['start_time'].' 00:00:00')];
        } else {
            $map[] = ['r_created_at', '>=', strtotime(date('Y-m-d',strtotime('-1 month')).' 00:00:00')];
        }
        //结束时间
        if ($request->input('end_time')) {
            $map[] = ['r_created_at', '<=', strtotime($param['end_time'].' 23:59:59')];
        } else {
            $map[] = ['r_created_at', '<=', time()];
        }
        //部门id
        if ($request->input('u_d_id')) {
            $where[] = ['u_d_id', $request->input('u_d_id')];
        }
        //员工ID
        if ($request->input('u_id')) {
            $where[] = ['u_id', $request->input('u_id')];
        }
        $res = $this->roomInfo_model->roomAchievementList($where, $map);

        $start = date('Y-m-d',strtotime('-1 month'));
        $end = date('Y-m-d',time());
        $data = [];
        foreach ($res as $k=>$v) {
            $num = $k+1;
            $v['ranking'] = '第'.$num.'名';
            array_push($data, $v);
        }
        return $this->success('ok', $data, $start, $end);
    }


    /**
     * @param Request $request
     * 获取员工录入客源信息排行榜
     */
    public function getCustomerInfoAchievementList(Request $request){
        $param = $request->all();
        $map = [];
        $where =[];
        //开始时间
        if ($request->input('start_time')) {
            $map[] = ['c_created_at', '>=', strtotime($param['start_time'].' 00:00:00')];
        } else {
            $map[] = ['c_created_at', '>=', strtotime(date('Y-m-d',strtotime('-1 month')).' 00:00:00')];
        }
        //结束时间
        if ($request->input('end_time')) {
            $map[] = ['c_created_at', '<=', strtotime($param['end_time'].' 23:59:59')];
        } else {
            $map[] = ['c_created_at', '<=', time()];
        }
        //部门id
        if ($request->input('u_d_id')) {
            $where[] = ['u_d_id', $request->input('u_d_id')];
        }
        //员工ID
        if ($request->input('u_id')) {
            $where[] = ['u_id', $request->input('u_id')];
        }
        $res = $this->customers_model->customersAchievementList($map, $where);
        $start = date('Y-m-d',strtotime('-1 month'));
        $end = date('Y-m-d',time());
        $data = [];
        foreach ($res as $k=>$v) {
            $num = $k+1;
            $v['ranking'] = '第'.$num.'名';
            array_push($data, $v);
        }
        return $this->success('ok', $data, $start, $end);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取员工房源跟进记录排行
     */
    public function getRoomFollowNum(Request $request){
        $param = $request->all();
        $map = [];
        $where =[];
        //开始时间
        if ($request->input('start_time')) {
            $map[] = ['f_created_at', '>=', strtotime($param['start_time'].' 00:00:00')];
        } else {
            $map[] = ['f_created_at', '>=', strtotime(date('Y-m-d',strtotime('-1 month')).' 00:00:00')];
        }
        //结束时间
        if ($request->input('end_time')) {
            $map[] = ['f_created_at', '<=', strtotime($param['end_time'].' 23:59:59')];
        } else {
            $map[] = ['f_created_at', '<=', time()];
        }
        //部门id
        if ($request->input('u_d_id')) {
            $where[] = ['u_d_id', $request->input('u_d_id')];
        }
        //员工ID
        if ($request->input('u_id')) {
            $where[] = ['u_id', $request->input('u_id')];
        }
        $res = $this->roomFollow_model->getRoomFollowNum($map,$where);
        $start = date('Y-m-d',strtotime('-1 month'));
        $end = date('Y-m-d',time());
        $data = [];
        foreach ($res as $k=>$v) {
            $num = $k+1;
            $v['ranking'] = '第'.$num.'名';
            array_push($data, $v);
        }
        return $this->success('ok', $data, $start, $end);
    }



    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取员工客源跟进记录排行
     */
    public function getCustomersFollowNum(Request $request){
        $param = $request->all();
        $map = [];
        $where = [];
        //开始时间
        if ($request->input('start_time')) {
            $map[] = ['cf_created_at', '>=', strtotime($param['start_time'].' 00:00:00')];
        } else {
            $map[] = ['cf_created_at', '>=', strtotime(date('Y-m-d',strtotime('-1 month')).' 00:00:00')];
        }
        //结束时间
        if ($request->input('end_time')) {
            $map[] = ['cf_created_at', '<=', strtotime($param['end_time'].' 23:59:59')];
        } else {
            $map[] = ['cf_created_at', '<=', time()];
        }
        //部门id
        if ($request->input('u_d_id')) {
            $where[] = ['u_d_id', $request->input('u_d_id')];
        }
        //员工ID
        if ($request->input('u_id')) {
            $where[] = ['u_id', $request->input('u_id')];
        }
        $res = $this->customersFollow_model->getCustomersFollowNum($map, $where);
        $start = date('Y-m-d',strtotime('-1 month'));
        $end = date('Y-m-d',time());
        $data = [];
        foreach ($res as $k=>$v) {
            $num = $k+1;
            $v['ranking'] = '第'.$num.'名';
            array_push($data, $v);
        }
        return $this->success('ok', $data, $start, $end);
    }

}